/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

class MathPartialSums {
    public static final double NUM_FLOAT_TWO_CONST = 2.0;
    public static final double NUM_FLOAT_THREE_CONST = 3.0;
    public static final double NUM_NEGATIVE_CONST = -0.5;
    public static final int NUM_INT_TWO_CONST = 2;
    public static final int NUM_WHILE_LOOP_CONST = 16384;
    public static final int NUM_SETIME_CONST = 1000;
    public static final int NUM_LOOP_COUNT_CONST = 80;
    public static double partial(int n) {
        double a1 = 0.0;
        double a2 = 0.0;
        double a3 = 0.0;
        double a4 = 0.0;
        double a5 = 0.0;
        double a6 = 0.0;
        double a7 = 0.0;
        double a8 = 0.0;
        double a9 = 0.0;
        double twothirds = NUM_FLOAT_TWO_CONST / NUM_FLOAT_THREE_CONST;
        double alt = -1.0;
        double k2 = 0.0;
        double k3 = 0.0;
        double sk = 0.0;
        double ck = 0.0;

        for (int m = 1; m <= n; m++) {
            k2 = m * m;
            k3 = k2 * m;
            sk = Math.sin(m);
            ck = Math.cos(m);
            alt = -alt;
            a1 += Math.pow(twothirds, m - 1);
            a2 += Math.pow(m, NUM_NEGATIVE_CONST);
            a3 += 1.0 / (m * (m + 1.0));
            a4 += 1.0 / (k3 * sk * sk);
            a5 += 1.0 / (k3 * ck * ck);
            a6 += 1.0 / m;
            a7 += 1.0 / k2;
            a8 += alt / m;
            a9 += alt / (NUM_INT_TWO_CONST * m - 1);
        }
        // NOTE: We don't try to validate anything from pow(),  sin() or cos() because those aren't
        // well-specified in ECMAScript;
//        System.out.println("a6 + a7 + a8 + a9 = " + (a6 + a7 + a8 + a9));
        return a6 + a7 + a8 + a9;
    }
}

/*
 *  @State
 *  @Tags Jetstream2
 */
class Benchmark {
    public static void run() {
        double total = 0;
        int n = 1024;
        while (n <= MathPartialSums.NUM_WHILE_LOOP_CONST) {
            total = total + MathPartialSums.partial(n);
            n = n * MathPartialSums.NUM_INT_TWO_CONST;
        }
//        System.out.println("total = " + total);
        double expected = 60.08994194659945;
        if (total != expected) {
            System.out.println("ERROR: bad result: expected" + expected + "but got" + total);
        }
    }

    /**
     * @Benchmark
     */
    public static void runIterationTime() {
        double start = System.nanoTime() / 1000_000.0;
        for (int i = 0; i < MathPartialSums.NUM_LOOP_COUNT_CONST; i++) {
            run();
        }
        double end = System.nanoTime() / 1000_000.0;
        System.out.println("math-partial-sums: ms = " + (end - start));
    }

    public static void main(String[] args) {
        runIterationTime();
    }
}
